<template>
  <div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    />
    <div class="doumee-table-buttons">
      <el-button :loading="exLoading" type="primary" @click="handleExport">{{
        translate("export")
      }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="translate('memberInfo')"
        prop="memberInfo"
        min-width="150"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('cph')"
        prop="plateNumber"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('discountType')"
        prop="type"
        min-width="120"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span v-if="scope.row.type == '1'">{{ translate("shichang") }}</span>
          <span v-else>{{ translate("other") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('yhsc')"
        prop="duration"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('lqVenue')"
        prop="venueName"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('lqsj')"
        prop="createTime"
        min-width="160"
        show-overflow-tooltip
      />
    </el-table>
    <Pagination
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
  </div>
</template>

<script>
import { pdMrExport, venueList, pdMrList } from '@/api'
import { parseTime } from '@/utils'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
export default {
  name: 'NoticeVenue',
  components: {
    Pagination
  },
  data() {
    return {
      queryFormConfig,
      exLoading: false,
      listLoading: false,
      list: [],
      meta: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      filters: {}
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getVenueList()
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    loadingTable() {
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
    },
    // 获取场馆列表
    getVenueList() {
      venueList({
        param: {}
      }).then(res => {
        if (res.errorCode !== '000000') return
        const arr = res.recordList
        this.queryFormConfig.formItems[0].options = arr
      })
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      this.listLoading = true
      pdMrList({ pagination, param }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList
          this.total = res.totalCount
          if (res.totalCount && res.recordList.length === 0 && pagination.page > 1) {
            this.getList(Math.ceil(res.totalCount / pagination.rows))
          }
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    },
    handleExport() {
      this.exLoading = true
      const { filters } = this
      const param = { ...filters }
      pdMrExport({ param })
        .then((res) => {
          this.exLoading = false
          if (res.errorCode === '000000') {
            window.open(res.record.showUrl, '_blank')
          }
        })
        .catch(() => {
          this.exLoading = false
        })
    }
  }
}
</script>

